package com.ychs.servlet;

import com.ychs.entity.*;
import com.ychs.service.IBaseService;
import com.ychs.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/7/25
 */
@WebServlet(value = "/UserServlet")
public class UserServlet extends BaseServlet {
    private IBaseService<User> service = new UserService();
    @Override
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter user add method");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String collegeId = request.getParameter("college");
        String majorId = request.getParameter("major");
        String clazz = request.getParameter("clazz");
        String grade = request.getParameter("grade");
        String schoolJob = request.getParameter("schoolJob");
        String phone = request.getParameter("phone");
        String QQ = request.getParameter("QQ");
        String roleId = request.getParameter("role");
        String state = request.getParameter("state");
        String remark = request.getParameter("remark");

        College college = new College();
        college.setId(Integer.parseInt(collegeId));
        Major major = new Major();
        major.setId(Integer.parseInt(majorId));
        Role role = new Role();
        role.setId(Integer.parseInt(roleId));

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setName(name);
        user.setCollege(college);
        user.setMajor(major);
        user.setClazz(clazz);
        user.setGrade(Integer.parseInt(grade));
        user.setSchoolJob(schoolJob);
        user.setPhone(phone);
        user.setQq(QQ);
        user.setRole(role);
        user.setState(state);
        user.setRemark(remark);

        boolean result = service.add(user);
        logger.info(result);
        String json = json(result);
        response.getWriter().write(json);
    }

    @Override
    protected void modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter user modify method");
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String collegeId = request.getParameter("college");
        String majorId = request.getParameter("major");
        String clazz = request.getParameter("clazz");
        String grade = request.getParameter("grade");
        String schoolJob = request.getParameter("schoolJob");
        String phone = request.getParameter("phone");
        String QQ = request.getParameter("QQ");
        String roleId = request.getParameter("role");
        String state = request.getParameter("state");
        String remark = request.getParameter("remark");

        College college = new College();
        college.setId(Integer.parseInt(collegeId));
        Major major = new Major();
        major.setId(Integer.parseInt(majorId));
        Role role = new Role();
        role.setId(Integer.parseInt(roleId));

        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setUsername(username);
        user.setPassword(password);
        user.setName(name);
        user.setCollege(college);
        user.setMajor(major);
        user.setClazz(clazz);
        user.setGrade(Integer.parseInt(grade));
        user.setSchoolJob(schoolJob);
        user.setPhone(phone);
        user.setQq(QQ);
        user.setRole(role);
        user.setState(state);
        user.setRemark(remark);

        boolean result = service.modify(user);
        logger.info(result);
        String json = json(result);
        response.getWriter().write(json);
    }

    @Override
    protected void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter user remove method");
        String id = request.getParameter("id");
        boolean result = service.remove(Integer.parseInt(id));
        logger.info(result);
        String json = json(result);
        response.getWriter().write(json);
    }

    @Override
    protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter user search method");
        Map<String, Object> conditions = new HashMap<>();

        String searchName = request.getParameter("searchName");
        String searchCollege = request.getParameter("searchCollege");
        String searchMajor = request.getParameter("searchMajor");
        String searchClazz = request.getParameter("searchClazz");
        String searchGrade = request.getParameter("searchGrade");

        if(searchName!=null&&!"".equals(searchName)){
            conditions.put("name",searchName);
        }
        if(searchCollege!=null&&!"".equals(searchCollege)){
            conditions.put("college",searchCollege);
        }
        if(searchMajor!=null&&!"".equals(searchMajor)){
            conditions.put("major",searchMajor);
        }
        if(searchClazz!=null&&!"".equals(searchClazz)){
            conditions.put("clazz",searchClazz);
        }
        if(searchGrade!=null&&!"".equals(searchGrade)){
            conditions.put("grade",searchGrade);
        }

        int count = service.searchTotalNumber(conditions);

        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        if (page != null && limit != null) {
            int begin = (Integer.parseInt(page) - 1) * Integer.parseInt(limit);
            int pageSize = Integer.parseInt(limit);
            conditions.put("begin", begin);
            conditions.put("pageSize", pageSize);
        }else{
            conditions.put("begin", 0);
            //2是为了方便测试，一般为10
            conditions.put("pageSize", 10);
        }

        List<User> members = service.search(conditions);
        String json = json(count, members);
        logger.info("json"+json);
        response.getWriter().write(json);
    }

    @Override
    protected void searchById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter user searchById method");
        String id = request.getParameter("id");
        User user = service.searchById(Integer.parseInt(id));
        String json = json(user);
        logger.info("searchById:"+json);
        response.getWriter().write(json);
    }
}
